﻿namespace OboutInc.ImageZoom.ImageFactory
{
    using System;
    using System.Drawing;
    using System.Drawing.Imaging;
    using System.IO;

    internal class ImageZoomResize
    {
        internal MemoryStream GetResizedImage(Bitmap imgIn, int width, int height, ImageFormat type)
        {
            double num = imgIn.Height;
            double num2 = imgIn.Width;
            double num3 = 1.0;
            if (width > 0)
            {
                num3 = ((double) width) / num2;
            }
            else if (height > 0)
            {
                num3 = ((double) height) / num;
            }
            Bitmap image = new Bitmap((int) (num2 * num3), (int) (num * num3));
            Graphics graphics = Graphics.FromImage(image);
            graphics.Clear(Color.White);
            graphics.DrawImage(imgIn, new Rectangle(0, 0, (int) (num3 * num2), (int) (num3 * num)), new Rectangle(0, 0, (int) num2, (int) num), GraphicsUnit.Pixel);
            MemoryStream stream = new MemoryStream();
            image.Save(stream, type);
            return stream;
        }

        internal MemoryStream GetResizedImageWithWaterMark(Bitmap imgIn, int width, int height, ImageFormat type, string watermarkText, bool isBig, Color watermarkColor, string watermarkFontFamily, int watermarkFontSize, FontStyle watermarkFontStyle)
        {
            double num = imgIn.Height;
            double num2 = imgIn.Width;
            double num3 = 1.0;
            if (width > 0)
            {
                num3 = ((double) width) / num2;
            }
            else if (height > 0)
            {
                num3 = ((double) height) / num;
            }
            Bitmap image = new Bitmap((int) (num2 * num3), (int) (num * num3));
            Graphics canvas = Graphics.FromImage(image);
            canvas.Clear(Color.White);
            canvas.DrawImage(imgIn, new Rectangle(0, 0, (int) (num3 * num2), (int) (num3 * num)), new Rectangle(0, 0, (int) num2, (int) num), GraphicsUnit.Pixel);
            ImageZoomWaterMark mark = new ImageZoomWaterMark();
            if (isBig)
            {
                mark.AddWaterMark(canvas, watermarkText, (int) (num2 * num3), (int) (num * num3));
            }
            else
            {
                mark.AddWaterMark(canvas, watermarkText, new Rectangle(0, 0, (int) (num3 * num2), (int) (num3 * num)), watermarkColor, watermarkFontFamily, watermarkFontSize, watermarkFontStyle);
            }
            MemoryStream stream = new MemoryStream();
            image.Save(stream, type);
            return stream;
        }
    }
}

